package com.example.onlineeducation.ui.auth

import android.content.Context
import android.content.Intent
import android.view.inputmethod.EditorInfo
import android.widget.TextView.OnEditorActionListener
import androidx.activity.ComponentActivity
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import com.example.onlineeducation.base.BaseActivity
import com.example.onlineeducation.data.model.RegisterSearchWorkTypeListDataModel
import com.example.onlineeducation.databinding.*
import com.example.onlineeducation.viewmodel.RegisterVM
import com.yc.jetpacklib.extension.ycInitLinearLayoutManage
import com.yc.jetpacklib.recycleView.YcPagingDataAdapter
import com.yc.jetpacklib.refresh.YcRefreshBaseUtil
import com.yc.jetpacklib.refresh.YcRefreshSpecialPagingUtil
import java.util.*


/**
 *  注册工种列表
 */
class RegisterSearchWorkTypeActivity :
    BaseActivity<RegisterProjectListAcitvityBinding>(RegisterProjectListAcitvityBinding::inflate) {

    companion object {
        const val KEY_ID = "key_id"
        const val KEY_NAME = "key_name"

        fun newInstance(activity: Context, launcher: ActivityResultLauncher<Intent>) {
            val intent = Intent(activity, RegisterSearchWorkTypeActivity::class.java).apply{
            }
            launcher.launch(intent)
        }
        @JvmStatic
        inline fun getLauncher(activity: ComponentActivity, crossinline block: (id: String, name: String) -> Unit): ActivityResultLauncher<Intent> {
            return activity.registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
                if (it.resultCode == RESULT_OK && it.data != null) {
                    val id = it.data?.getStringExtra(KEY_ID)!!
                    val name = it.data?.getStringExtra(KEY_NAME)!!
                    block.invoke(id,name)
                }
            }
        }
    }

    private val mAdapter: YcPagingDataAdapter<RegisterSearchWorkTypeListDataModel, RegisterSearchItemBinding> by YcPagingDataAdapter.ycLazyInitApply(
        RegisterSearchItemBinding::inflate, RegisterSearchWorkTypeListDataModel.diffCallback
    ) {
        mOnUpdate = { item ->
            tvProName.text = item.workTypeName
        }
        mItemClick = {
            val intent = Intent()
            intent.putExtra(KEY_ID, it.workTypeId)
            intent.putExtra(KEY_NAME, it.workTypeName)
            setResult(RESULT_OK, intent)
            finish()
        }
    }

    private val mViewModel: RegisterVM by ycViewModels()
    private lateinit var mRefreshUtil: YcRefreshBaseUtil<RegisterSearchWorkTypeListDataModel>

    override fun RegisterProjectListAcitvityBinding.initView() {
        actionbarLayout.setTitle("工种搜索")
        etSearch.hint = "搜索工种"
        rvProList.apply {
            adapter = mAdapter
            ycInitLinearLayoutManage()
        }
        etSearch.setOnEditorActionListener { _, keyCode, _ ->
            when (keyCode) {
                EditorInfo.IME_ACTION_SEARCH -> mRefreshUtil.startRefresh(true)
            }
            false
        }
        tvCancel.setOnClickListener {
            finish()
        }
        mRefreshUtil =
            YcRefreshSpecialPagingUtil<RegisterSearchWorkTypeListDataModel>(this@RegisterSearchWorkTypeActivity)
                .build(mAdapter, srlList, rvProList, flRefresh) {
                    mRefreshCall = {
                        getData()
                    }
                }
        mViewModel.mWorkTypeList.observe {
            mRefreshUtil.acSetPagingData(it)
        }
    }

    private fun RegisterProjectListAcitvityBinding.getData() {
        mViewModel.getWorkTypeList(etSearch.text.toString())
    }


}